package com.bootdo.company.controller;

import com.bootdo.common.utils.R;
import com.bootdo.system.domain.ContentDO;
import com.bootdo.system.domain.MenuDO;
import com.bootdo.system.service.ContentService;
import com.bootdo.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Controller
public class IndexController extends IndexBaseController{

    @Autowired
    MenuService menuService;

    @Autowired
    private ContentService contentService;

    @GetMapping({"/", ""})
    public String index( Model model){
        /**
         * 获取菜单信息
         */
        getHeadMenu(model);
        /**
         * 获取解决方案数据
         */
        List<MenuDO> jjList = getJJList(model);
        model.addAttribute("jjList",jjList);
        List<ContentDO> dataList = new ArrayList<>();
        getSingleInfoList(jjList, dataList);
        model.addAttribute("dataList",dataList);

        return "company/index";
    }



    @ResponseBody
    @GetMapping("/blog/query/{id}")
    public ContentDO query(@PathVariable("id") Integer id, Model model){
        ContentDO content = contentService.get(id);
        return content;
    }

    @ResponseBody
    @GetMapping("/blog/menu/{id}")
    public List<ContentDO> queryList(@PathVariable("id") Long id,Model model){
        HashMap<String,Object> dataMap = new HashMap<>();
        dataMap.put("webMenuid",id);
        List<ContentDO> dataList = contentService.list(dataMap);
        return dataList;
    }

    @GetMapping("/blog/views")
    public String views( Model model,Integer contentId){
        /**
         * 获取菜单信息
         */
        getHeadMenu(model);
        ContentDO data = contentService.get(contentId);

        model.addAttribute("data",data);
        return "company/details";
    }


    @GetMapping("/blog/viewMenu")
    public String viewMenu( Model model,Long menuId){
        /**
         * 获取菜单信息
         */
        getHeadMenu(model);
        /**
         * 获取解决方案数据
         */
        List<MenuDO> jjList = getJJList(model);
        model.addAttribute("jjList",jjList);
        List<ContentDO> dataList = new ArrayList<>();
        getSingleInfoList(jjList, dataList);
        model.addAttribute("dataList",dataList);

        String viewPath = "company/index";
        MenuDO menu = menuService.get(menuId);
        if(Objects.equals(menu.getDataType(),1)) {//图文列表
            MenuDO data = menuService.get(menuId);
            model.addAttribute("data",data);

            HashMap<String,Object> dataMap = new HashMap<>();
            dataMap.put("webMenuid",menuId);
            List<ContentDO> infoList = contentService.list(dataMap);
            model.addAttribute("infoList",infoList);
            viewPath = "company/news";
        }else if(Objects.equals(menu.getDataType(),3)) {//案例列表
            MenuDO data = menuService.get(menuId);
            model.addAttribute("data",data);

            HashMap<String,Object> dataMap = new HashMap<>();
            dataMap.put("webMenuid",menuId);
            List<ContentDO> infoList = contentService.list(dataMap);
            model.addAttribute("infoList",infoList);
            viewPath = "company/case";
        }else if(Objects.equals(menu.getDataType(),2)) {//单页
            ContentDO data = contentService.getContent(menuId);
            model.addAttribute("data",data);
            viewPath = "company/newsDetail";
        }
        return viewPath;
    }

    private void getSingleInfoList(List<MenuDO> jjList, List<ContentDO> dataList) {
        for (MenuDO menuDO : jjList) {
            HashMap<String, Object> contentParam = new HashMap<String, Object>();
            contentParam.put("webMenuid", menuDO.getMenuId());
            List<ContentDO> contentDOList = contentService.list(contentParam);
            if (contentDOList != null && contentDOList.size() > 0) {
                dataList.add(contentDOList.get(0));
            }
        }
    }

}
